#include <math.h>

int max_array(char a[])
{
    int max = a[0];
    int i=0;
    for (i=0; i<9; i++)
    {
        if ((unsigned int) a[i] > max)
	    max=a[i];
    }
    return (char) max;
}

char get_max(unsigned int indice_fila, unsigned int indice_columna,
			 unsigned char* src, int src_row_size, int offset) {

    char values[9] = {
	src[(indice_fila - 1) * src_row_size + (indice_columna - 1) * 3 + offset],
	src[(indice_fila - 1) * src_row_size + indice_columna * 3 + offset],
	src[(indice_fila - 1) * src_row_size + (indice_columna + 1) * 3 + offset],
	src[indice_fila * src_row_size + (indice_columna - 1) * 3 + offset],
	src[indice_fila * src_row_size + indice_columna * 3 + offset],
	src[indice_fila * src_row_size + (indice_columna + 1) * 3 + offset],
	src[(indice_fila + 1) * src_row_size + (indice_columna - 1) * 3 + offset],
	src[(indice_fila + 1) * src_row_size + indice_columna * 3 + offset],
	src[(indice_fila + 1) * src_row_size + (indice_columna + 1) * 3 + offset]
    };	

    char max = max_array(values);
    return max;
}

char get_max_r(unsigned int indice_fila, unsigned int indice_columna,
			   unsigned char* src, int src_row_size) {
    return get_max(indice_fila, indice_columna, src, src_row_size, 2);
}

char get_max_g(unsigned int indice_fila, unsigned int indice_columna,
			   unsigned char* src, int src_row_size) {
    return get_max(indice_fila, indice_columna, src, src_row_size, 1);
}

char get_max_b(unsigned int indice_fila, unsigned int indice_columna,
			   unsigned char* src, int src_row_size) {
    return get_max(indice_fila, indice_columna, src, src_row_size, 0);
}

void colorizar_c (
	unsigned char *src,
	unsigned char *dst,
	int m,
	int n,
	int src_row_size,
	int dst_row_size,
	float alpha
) {

    int indice_fila = 1;
    int indice_columna = 1;
    
    while (indice_fila < m - 1) {
        while (indice_columna < n - 1) {
			unsigned int max_r = get_max_r(indice_fila, indice_columna, src, src_row_size);
			unsigned int max_g = get_max_g(indice_fila, indice_columna, src, src_row_size);
			unsigned int max_b = get_max_b(indice_fila, indice_columna, src, src_row_size);

            if (indice_columna == 230 && indice_fila == 174) {
                float fi_r = 1 - alpha;
            }
                
    

            float fi_r = 1 - alpha;
            float fi_g = 1 - alpha;
            float fi_b = 1 - alpha;			
            if (max_r >= max_g && max_r >= max_b)
                fi_r = 1 + alpha;

            if (max_r < max_g && max_g >= max_b)
                fi_g = 1 + alpha;

            if (max_r < max_b && max_g < max_b)
                fi_b = 1 + alpha;
                
            unsigned int src_r = (unsigned int) src[indice_fila * src_row_size + indice_columna * 3 + 2];
            unsigned int src_g = (unsigned int) src[indice_fila * src_row_size + indice_columna * 3 + 1];
            unsigned int src_b = (unsigned int) src[indice_fila * src_row_size + indice_columna * 3 + 0];

            float dest_r = round(src_r * fi_r);
            float dest_g = round(src_g * fi_g);
            float dest_b = round(src_b * fi_b);

            if (dest_r > 255)
                dest_r = 255;
            if (dest_g > 255)
                dest_g = 255;
            if (dest_b > 255)
                dest_b = 255;

            dst[indice_fila * src_row_size + indice_columna * 3 + 2] = (char) dest_r;
            dst[indice_fila * src_row_size + indice_columna * 3 + 1] = (char) dest_g;
            dst[indice_fila * src_row_size + indice_columna * 3 + 0] = (char) dest_b;
                
            indice_columna++;	
		}
		indice_columna = 1;
		indice_fila++;	
	}


}
